﻿
Imports System.IO
Imports std
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl
Imports storoomc

Public Class blog
    Inherits requestProcessor

    Public Const _pathidentity As String = "/blog"

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Public Overrides Function process(ByVal context As httpContext) As Boolean
        Dim list As vector(Of String) = Nothing
        If storoom.list(list) AndAlso Not list Is Nothing AndAlso list.size() > 0 Then
            Dim bloglink As String = Nothing
            Dim filter As String = Nothing
            filter = config.parameter(configNaming.blog.name, configNaming.blog.linkfilter _
                                      , configNaming.defaultValue.blog.linkfilter)
            Do
                bloglink = list(rnd(0, list.size(), True))
            Loop Until fitfilter(filter, bloglink)

            Dim buff As MemoryStream = Nothing
            buff = context.buff()

            Dim page As xhtmlpage = Nothing
            page = New xhtmlpage(buff)
            page.title().text = applicationSign() + " - blog"
            page.body().id = "id_blog"

            Dim csslink As csslink = Nothing
            csslink = New csslink(buff)
            csslink.href = csslinkhref
            page.head().addchild(csslink)
            iframeJumpKeeper.invoke(page)

            Dim label As label = Nothing
            label = New label(buff)
            label.text = "random selected blog with link - "
            label.id = "id_selectlabel"
            page.body().addchild(label)

            Dim a As a = Nothing
            a = New a(buff)
            a.text = bloglink
            a.href = bloglink
            a.id = "id_selecta"
            a.target = "_blank"
            page.body().addchild(a)

            Dim iframe As autoheightIframe = Nothing
            iframe = New autoheightIframe(buff, page.inlineScript())
            iframe.src = bloglink
            iframe.frameborder = 0
            iframe.marginheight = 0
            iframe.marginwidth = 0
            iframe.scrolling = xhtmlControl_1_1.scrolling_enum.auto
            page.body().addchild(iframe)

#If retired Then
            iframe = New autoheightFrame(buff)
            iframe.src = strmid(comment._pathidentity, strlen(network.pathSeperator)) + network.questionMark _
                        + "key" + network.argumentNameValueSeperator + "blogcomment_" + urlUtils.htmlencode(bloglink)
            page.body().addchild(iframe)
#End If

            Dim reloader As reloader = Nothing
            reloader = New reloader(buff)
            reloader.id = "id_reload"
            reloader.text = "try another blog"
            page.body().addchild(reloader)

            context.setstatuscode(Net.HttpStatusCode.OK, "OK")
            context.setcontentEncoding(encoding.utf8)
            context.setcontentType(network.http.contentType.texthtmlUtf8)

            If page.render() Then
                Return True
            Else
                raiseError("cannot render xhtmlpage.", errorHandle.errorType.warning)
                Return False
            End If
        Else
            raiseError("cannot get list from storoom, ignore request.", errorHandle.errorType.warning)
            Return False
        End If
    End Function
End Class
